import Reflux from "reflux";
import GUtils from "@/utils/ajax";
import {Message} from "@biz/Components";

const BaseAction = {
    search: { asyncResult: true },
    create: { asyncResult: true },
    getDetail: { asyncResult: true },
    edit: { asyncResult: true },
    delete: { asyncResult: true },
    enabled: { asyncResult: true },
    export:{asyncResult: true},
    sort:{asyncResult: true},

    preCreate: {},
    goCreate:{},
    preEdit: {},
    preDetail: {},


    resetSo: {},
    changeCurrent: {},
    changePageSize: {},
    rowSelectChange: {},

    cancel: {},

    setNewState:{},
    cancelModal:{},
    unmount: {},
}
const addDefaultListerns=(Action, api)=>{
    if(api.search){
        Action.search.listen(function (so) {
            GUtils.jsonAjax.post(api.search, so).then(this.completed, this.failed);
        });
    }

    if(api.detail) {
        Action.getDetail.listen(function (id, callback) {
            GUtils.Ajax.post(api.detail, {id}).then(this.completed.bind(this, callback), this.failed);
        });
    }

    if(api.create) {
        Action.create.listen(function (vo) {
            GUtils.jsonAjax.post(api.create, vo).then(this.completed.bind(this), this.failed);
        });
    }

    if(api.edit) {
        Action.edit.listen(function (vo) {
            GUtils.jsonAjax.post(api.edit, vo).then(this.completed.bind(this), this.failed);
        });
    }

    if(api.enabled) {
        Action.enabled.listen(function (id, enabled) {
            GUtils.Ajax.post(api.enabled, {id, enabled}).then(this.completed, this.failed);
        });
    }
    if(api.delete) {
        Action.delete.listen(function (id) {
            GUtils.Ajax.post(api.delete, {id}).then(this.completed, this.failed);
        });
    }
    if(api.sort) {
        Action.sort.listen(function (id1, id2) {
            GUtils.Ajax.post(api.sort, {id1, id2}).then(this.completed, this.failed);
        });
    }

}






const PublicAction = Reflux.createActions({
    // 店铺列表
    allShopList: {asyncResult: true},
    // 商家账号列表
    allShopAccountList: {asyncResult: true},
    // 商家账号列表
    allWashProductList: {asyncResult: true},
});

const api = {
    allShopList: "/data/shopList",
    allShopAccountList: "/data/shopAccountList",
    allWashProductList: "/data/washProductList"
};
let cacheData = {}
const getFromCache = (field, callback, api, params) => {
    if (cacheData[field]) {
        callback && callback({ voList: cacheData[field] })
        return
    }
    GUtils.Ajax.get(api, params).then(dealRes.bind(this, field, callback))
   /* if (isform) {
        GUtils.Ajax.get(api, params).then(dealRes.bind(this, field, callback))
    } else {
        GUtils.jsonAjax.post(api, params).then(dealRes.bind(this, field, callback))
    }*/
}
const dealRes = (field, callback, res) => {
    if (res.success) {
        cacheData[field] = res.voList
        callback && callback(res)
    } else {
        Message.error(res.errMsg)
    }
}
PublicAction.clearCacheData = function () {
    cacheData = {}
}
PublicAction.clearFieldData = function (field) {
    cacheData[field] = null
}
// 店铺列表
PublicAction.allShopList.listen(function (callback) {
    getFromCache("allShopList", callback, api.allShopList, { })
});
// 商家账号列表
PublicAction.allShopAccountList.listen(function (callback) {
    getFromCache("allShopAccountList", callback, api.allShopAccountList, { })
})

// 洗车产品列表
PublicAction.allWashProductList.listen(function (callback) {
    getFromCache("allWashProductList", callback, api.allWashProductList, { })
})


export { BaseAction as default, PublicAction, addDefaultListerns }